<?php
namespace app\admin\controller;
use think\Controller;

class Car extends Base
{
	/**
	 * 品牌列表
	 */
	public function brandlist(){
		//是否推荐
		if(input("ic") !== null){
			model("Brand")->where("bid",input("id"))->update(['is_commend'=>input("ic")]);
		}
		//排序
		if(input("sort") !== null){
			model("Brand")->where("bid",input("id"))->update(['sort'=>input("sort")]);
		}
		//是否展示
		if($this->request->isPost() && isset($_POST['lock'])){
			if(input('post.lock')==1){
				model("Brand")->where('bid='.input('post.id'))->update(['is_show'=>0]);
			}else{
				model("Brand")->where('bid='.input('post.id'))->update(['is_show'=>1]);
			};
			return $this->success('1');
		}
		$condition=[];
		if(input('bname')){
			$condition[]=['bname','like',"%".input('bname')."%"];
		}
		$info = model("Brand")
				->order(['first','sort'=>'desc'])
				->where($condition)
				->paginate([
					'list_rows'=>10,
					'query'=>input('param.'),
				]);
		$this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 品牌添加
	 */
	public function brandadd(){
		if(request()->file('image') !== null){
			$file = request()->file('image');
			$info = $file->move('./static/admin/car/brand/');
			$path = "./static/admin/car/brand/".$info->getSaveName();
			$im = "/static/admin/car/brand/".$info->getSaveName();
			if(input('img') != $path && is_file(input('img'))){
				unlink(input('img'));
			}
			return json_encode(['path'=>$path,'im'=>$im]);
		}
		
		if($this->request->isPost()){
			if(input('delim')){
				unlink(".".input('delim'));
				return $this->success('1');
			}
			$result=$this->validate(input('post.'),'Brand');
			if($result!==true){
				return $this->error($result);
			}
			if(input('iscom') != null){
				$iscom = 1;
			}else{
				$iscom  = 0;
			}
			$logo = substr(input('img'),1);
			$arr = [
				"bname"      => input("bname"),
				"logo"       => $logo,
				"first"      => ucfirst(pinyin(input('bname'),'one')),
				"sort"       => 0,
				"is_show"    => 1,
				"is_commend" => $iscom,
			];
			$re = model('Brand')->add($arr);
			if($re > 0){
				return $this->success('添加成功');
			}else{
				return $this->error('添加失败');
			}
		}else{
			return $this->fetch();
		}
	}

	/**
	 * 品牌修改
	 */
	public function brandedit(){
		$info = model('Brand')->where("bid",input("bid"))->select();
		/**
		 * $im 数据库图片地址路径 $path用于删除操作的相对路径
		 */
		if(request()->file('image') !== null){
			$logo = ".".$info[0]['logo'];
			$file = request()->file('image');
			$info = $file->move('./static/admin/car/brand/');
			$path = "./static/admin/car/brand/".$info->getSaveName();
			$im = "/static/admin/car/brand/".$info->getSaveName();
			if($logo != input('img')){
				if(input('img') != $path && is_file(input('img'))){
					unlink(input('img'));
				}
			}
			return json_encode(['path'=>$path,'im'=>$im]);
		}
		if($this->request->isPost()){
			if(input('delim') != null){
				if($info[0]['logo'] != input('delim')){
					unlink(".".input('delim'));
					return $this->success('1');
				}else{
					return $this->success('1');
				}		
			}
			$result=$this->validate(input('post.'),'Brand');
			if($result!==true){
				return $this->error($result);
			}
			if(input('iscom') != null){
				$iscom = 1;
			}else{
				$iscom  = 0;
			}

			$logo = substr(input('img'),1);
			if($logo != $info[0]['logo']){
				unlink(".".$info[0]['logo']);
			}
			$arr = [
				"bname"      => input("bname"),
				"logo"       => $logo,
				"first"      => ucfirst(pinyin(input('bname'),'one')),
				"sort"       => 0,
				"is_show"    => 1,
				"is_commend" => $iscom,
			];
			$re = model('Brand')->where("bid",input("bid"))->update($arr);

			if($re > 0){
				return $this->success('修改成功');
			}else{
				return $this->error('未作任何修改');
			}
			dump(input());
		}else{
			
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	/**
	 * 品牌删除
	 */
	public function branddel(){
		$id=input('post.id');
		if(!empty(input('post.allid'))){
    		$id=substr(input('post.allid'),0,-1);
    	}
    	$logo = model('Brand')->getImg($id);
    	foreach ($logo as $v) {
    		if(substr($v['logo'],0,4) != "http"){
    			if(is_file(".".$v['logo'])){
	    			unlink(".".$v['logo']);
	    		}
    		}
    	}
    	$re=model('Brand')->where("bid","in",$id)->delete();
    	if($re > 0){
    		return $this->success('删除成功');
    	}else{
    		return $this->error('删除失败');
    	}
	}

	/**
	 * 车系管理列表
	 */
	public function cate(){
		//是否展示
		if($this->request->isPost() && isset($_POST['lock'])){
			//dump(input());
			if(input('post.lock')==1){
				model("Cate")->where('id='.input('post.id'))->update(['is_show'=>0]);
			}else{
				model("Cate")->where('id='.input('post.id'))->update(['is_show'=>1]);
			};
			return $this->success('1');
		}
		//排序
		if(input("sort") !== null){
			model("Cate")->where("id",input("id"))->update(['sort'=>input("sort")]);
		}
		//搜索功能
		$condition=[];
		if(input('name')){
			$condition[]=['name','like',"%".input('name')."%"];
		}
		$info = model('cate')
			->alias('c')
			->field("c.id,c.name,c.gname,c.sort,c.is_show,a.bname")
			->join('tp_brand a','a.bid=c.bid')
			->order(['gname','c.sort'=>'desc'])
			->where($condition)
			->paginate([
				'list_rows'=>10,
				'query'=>input('param.'),
			]);
		$this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 车系管理列表--添加
	 */
	public function cateadd(){
		if($this->request->isPost()){

			$result=$this->validate(input('post.'),'Cate');
			if($result!==true){
				return $this->error($result);
			}
			$re = model("Cate")->insert([
				"name"    => input("post.name"),
				"gname"   => input("post.gname"),
				"sort"    => 0,
				"is_show" => 1,
				"bid"     => input("post.bid"),
			]);
			if($re > 0){
				return $this->success("添加成功");
			}else{
				return $this->error("添加失败");
			}
		}else{
			$brand = model("Brand")->order(['first'])->select();
			$this->assign("brand",$brand);
			return $this->fetch();
		}
	}

	/**
	 * 车系管理列表--修改
	 */
	public function cateedit(){
		if($this->request->isPost()){

			$result=$this->validate(input('post.'),'Cate');
			if($result!==true){
				return $this->error($result);
			}
			$re = model("Cate")->where("id",input("post.id"))->update([
				"name"    => input("post.name"),
				"gname"   => input("post.gname"),
				"bid"     => input("post.bid"),
			]);
			if($re > 0){
				return $this->success("修改成功");
			}else{
				return $this->error("您没有做任何修改");
			}
		}else{
			$info = model("Cate")->where('id',input('id'))->select();
			$brand = model("Brand")->order(['first'])->select();
			$this->assign("brand",$brand);
			$this->assign("info",$info);
			return $this->fetch();
		}
	}

	/**
	 * 车系管理列表--删除
	 */
	public function catedel(){	
		$id=input('post.id');
		if(!empty(input('post.allid'))){
    		$id=substr(input('post.allid'),0,-1);
    	}
    	$re=model('Cate')->where("id","in",$id)->delete();
    	if($re > 0){
    		return $this->success('删除成功');
    	}else{
    		return $this->error('删除失败');
    	}
	}

	/**
	 * 车型管理列表
	 */
	public function type(){
		//是否展示
		if($this->request->isPost() && isset($_POST['lock'])){
			//dump(input());
			if(input('post.lock')==1){
				model("Cartype")->where('tid='.input('post.id'))->update(['is_show'=>0]);
			}else{
				model("Cartype")->where('tid='.input('post.id'))->update(['is_show'=>1]);
			};
			return $this->success('1');
		}
		//排序
		if(input("sort") !== null){
			model("Cartype")->where("tid",input("id"))->update(['sort'=>input("sort")]);
		}
		//搜索功能
		$condition=[];
		if(input('tname')){
			$condition[]=['tname','like',"%".input('tname')."%"];
		}
		$info = model("Cartype")
			->alias("t")
			->field("t.tid,t.tname,t.sort,t.is_show,c.name,c.gname,b.bname")
			->join('tp_cate c','t.cid=c.id')
			->join('tp_brand b','c.bid=b.bid')
			->order(['t.sort'=>'desc','c.gname','c.name'])
			->where($condition)
			->paginate([
				'list_rows'=>10,
				'query'=>input('param.'),
			]);
		$this->assign("info",$info);
		return $this->fetch();
	}

	/**
	 * 车型管理--添加
	 */
	public function typeadd(){
		if($this->request->isPost()){
			if(input('cid')==null){
				return $this->error('请选择车系');
			}
			$re = model("Cartype")->insert([
				'tname'        => input('post.tname'),
				'newprice'     => input('post.newprice'),
				'cc'           => input('post.cc'),
				'emst'         => input('post.emst'),
				'transmission' => input('post.transmission'),
				'cid'          => input('post.cid'),
				'lid'          => input('post.lid'),
			]);
			if($re > 0){
				return $this->success('添加成功');
			}else{
				return $this->error('添加失败');
			}
		}else{
			$brand = model("Brand")->order(['first'])->select();
			if(input('select')==1){
				$bid = input('bid');
			}else{
				$bid = $brand[0]['bid'];
			}
			$cate = model("Cate")->where("bid",$bid)->order(['gname'])->select();
			if(input('select')==1){
				return $cate;
			}
			$level = model("Level")->order(['sort'=>'desc'])->select();
			$this->assign('level',$level);
			$this->assign('brand',$brand);
			$this->assign('cate',$cate);
			return $this->fetch();
		}
	}

	/**
	 * 车型管理--修改
	 */
	public function typeedit(){
		if($this->request->isPost()){
			if(input('cid')==null){
				return $this->error('请选择车系');
			}
			$re = model("Cartype")->where('tid',input('tid'))->update([
				'tname'        => input('post.tname'),
				'newprice'     => input('post.newprice'),
				'cc'           => input('post.cc'),
				'emst'         => input('post.emst'),
				'transmission' => input('post.transmission'),
				'cid'          => input('post.cid'),
				'lid'          => input('post.lid'),
			]);
			if($re > 0){
				return $this->success('修改成功');
			}else{
				return $this->error('未作任何修改');
			}
		}else{
			$info = model("Cartype")
				->alias("t")
				->field("t.tid,t.tname,t.newprice,t.cc,t.transmission,t.emst,t.cid,t.lid,b.bid")
				->join('tp_cate c','t.cid=c.id')
				->join('tp_brand b','c.bid=b.bid')
				->where('tid',input('tid'))
				->select();
			$brand = model("Brand")->order(['first'])->select();
			if(input('select')==1){
				$bid = input('bid');
			}else{
				$bid = $info[0]['bid'];
			}
			$cate = model("Cate")->where("bid",$bid)->order(['gname'])->select();
			if(input('select')==1){
				return $cate;
			}
			$level = model("Level")->order(['sort'=>'desc'])->select();
			$this->assign('level',$level);
			$this->assign('cate',$cate);
			$this->assign('brand',$brand);
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	/**
	 * 车型管理--删除
	 */
	public function typedel(){
		$id=input('post.id');
		if(!empty(input('post.allid'))){
    		$id=substr(input('post.allid'),0,-1);
    	}
    	$re=model('Cartype')->where("tid","in",$id)->delete();
    	if($re > 0){
    		return $this->success('删除成功');
    	}else{
    		return $this->error('删除失败');
    	}
	}

	/**
	 * 车级管理--列表
	 */
	public function level(){
		//是否展示
		if($this->request->isPost() && isset($_POST['lock'])){
			//dump(input());
			if(input('post.lock')==1){
				model("Level")->where('lid='.input('post.id'))->update(['is_show'=>0]);
			}else{
				model("Level")->where('lid='.input('post.id'))->update(['is_show'=>1]);
			};
			return $this->success('1');
		}
		//排序
		if(input("sort") !== null){
			model("Level")->where("lid",input("id"))->update(['sort'=>input("sort")]);
		}
		//搜索功能
		$condition=[];
		if(input('lname')){
			$condition[]=['lname','like',"%".input('lname')."%"];
		}
		$info = model("Level")->where($condition)->order(['sort'=>'desc'])->paginate([
				'list_rows'=>10,
				'query'=>input('param.'),
			]);
		$this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 车级管理--添加
	 */
	public function leveladd(){
		if($this->request->isPost()){
			$result=$this->validate(input('post.'),'Level');
			if($result!==true){
				return $this->error($result);
			}
			$re = model('Level')->insert(['lname'=>input('post.lname')]);
			if($re > 0){
				return $this->success('添加成功');
			}else{
				return $this->error('添加失败');
			}

		}else{
			return $this->fetch();
		}
	}

	/**
	 * 车级管理--修改
	 */
	public function leveledit(){
		if($this->request->isPost()){
			$result=$this->validate(input('post.'),'Level');
			if($result!==true){
				return $this->error($result);
			}
			$re = model('Level')->where('lid',input('lid'))->update(['lname'=>input('post.lname')]);
			if($re > 0){
				return $this->success('修改成功');
			}else{
				return $this->error('未作任何修改');
			}

		}else{
			$info = model('Level')->where('lid',input('lid'))->select();
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	/**
	 * 车级管理--删除
	 */
	public function leveldel(){
		$id=input('post.id');
		if(!empty(input('post.allid'))){
    		$id=substr(input('post.allid'),0,-1);
    	}
    	$re=model('Level')->where("lid","in",$id)->delete();
    	if($re > 0){
    		return $this->success('删除成功');
    	}else{
    		return $this->error('删除失败');
    	}
	}
}